<?php
/**
 * View Nuovo Contatto - LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    16-mag-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript">
//<![CDATA[
    // Plugin: legge il metadata validate dal tag input e imposta i parametri del validator
    $.metadata.setType("attr", "validate");
    
    $(document).ready(function() {

	var validator = $("#partnerForm").validate({
	    //meta : 'attr',
	    invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
		    $("li#errorLi").show();
		} else {
		    $("li#errorLi").hide();
		};
	    },
	    
	    // Assegna classe 'error' al <li> dell'elemento invalido
	    errorPlacement: function(error, element) {
		//console.log(element);
		element.parent("span").parent("div").parent("li").addClass("error");
		error.appendTo( element.parent("span").parent("div"));
	    },
	    submitHandler: function(form) {
		//$('#salva').attr('disabled', 'disabled');
		form.submit();
	    },	    
	    errorElement: "p",
	    groups: {
		partner_phone: "partner_phone_prefix partner_phone_number",
		partner_fax: "partner_fax_prefix partner_fax_number",
		address: "partner_city partner_street partner_street_number partner_loc partner_province_id partner_cap"
	      },
	    rules: {
		partner_name:{
		    required: true
		},
		/* Info Opzionali */
		partner_email: {
		  email: true
		},
		partner_phone_prefix: {
		    digits: true,
		    rangelength: [1, 4]
		},
		partner_phone_number: {
		    digits: true,
		    rangelength: [1, 10]
		},
		partner_fax_prefix: {
		    digits: true,
		    rangelength: [1, 4]
		},
		partner_fax_number: {
		    digits: true,
		    rangelength: [1, 10]
		},
		partner_cap:{
		    digits: true,
		    rangelength: [5, 5]
		},
		partner_city:{
		    required: {
			depends: function(element) {
			    return ($('#partner_street').val() != '' || 
				    $('#partner_street_number').val() != '' );
			}
		    }
		},
		partner_province_id:{
		    required: {
			depends: function(element) {
			    return ($('#partner_street').val() != '' || 
				    $('#partner_street_number').val() != '' ||
				    $('#partner_city').val() != '');
			}
		    }
		}		
	    },
	    messages: {
		partner_name: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		partner_types: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		/* Info Opzionali */
		partner_email : {
		    email: "<?php echo  ERR_EMAIL ?>"
		},
		partner_phone_prefix: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel.' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel.' )?>"
		},
		partner_phone_number: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>"
		},
		partner_fax_prefix: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>"
		},
		partner_fax_number: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>"
		},
		partner_cap: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>"
		},	
		partner_street: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		partner_city: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		partner_province_id: {
		    required: "<?php echo  ERR_EMPTY ?>"
		}
	    },
	    debug:true
	});
    });
    
    $("#salva").click(function() {
      validator.form();
    });
    
    //]]>
</script>

<div class="info">
    <h2>Inserisci Nuovo Partner</h2>
    <div>Per inserire un nuovo post...</div>
    <div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
</div>

<form class="fab" name="partnerForm" id="partnerForm" action="/admin/partners/nuovo" method="post" enctype="multipart/form-data">
    <ul>
	<li class="paging-context ">
	    <table class="pgStyle1 circle2" cellspacing="0">
		<tr>
		    <td class="c selected">
			<var>1</var>
		    </td>
		    <td class="t">
			<b id="pagecircle1">Informazioni Generali</b>
		    </td>
		    <td class="c ">
			<var>2</var>
		    </td>

		    <td class="t">
			<b id="pagecircle2">Contatti</b>
		    </td>
		</tr>
	    </table>
	</li>
	<li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none'?>">
	    <h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
	    </p>
	</li>
	<li class="<?php echo  $this->errorMsg['partner_name'] ? 'error' : '' ?>">
	    <div class="float-left">
		<label class="desc <?php echo  $this->errorMsg['partner_name'] ? 'error' : '' ?>">Denominazione<span class="req">*</span></label>
		<span>
		    <input id="partner_firstname" name="partner_name" class="field text medium" size="20" type="text" maxlength="255" value="<?php echo  $this->data['partner_name'] ?>" />
		</span>
	    </div>
	    <p class="error" style="<?php echo  $this->errorMsg['partner_name'] ? 'display:block' : '' ?>"><?php echo  $this->errorMsg['partner_name'] ?></p>
	</li>

	<li class="<?php echo  $this->errorMsg['partner_types'] ? 'error' : '' ?>">
	    <div class="float-left">
		<label class="desc <?php echo  $this->errorMsg['partner_types'] ? 'error' : '' ?>">Tipo<span class="req">*</span></label>
		<span class="input">
		<?php foreach( $this->partner_types AS $k => $type ): ?>
		    <input validate="{required:true, minlength:1,messages:{required:'<?php echo ERR_EMPTY?>'}}" class="field checkbox" name="partner_types[]" id="<?php echo $k?>" type="checkbox" value="<?php echo $k?>" <?php echo  $this->data['partner_types'] AND in_array( $k, $this->data['partner_types'] ) ? 'checked="checked"' : '' ?> />
		    <label for="<?php echo $k?>" class="choice"><?php echo $type?></label>
		<?php endforeach; ?>
		</span>
	    </div>
	    <p class="error" style="<?php echo  $this->errorMsg['partner_types'] ? 'display:block' : '' ?>"><?php echo  $this->errorMsg['partner_types'] ?></p>
	</li>

	<li class="section">
	    <h3>Altre Info</h3>
	</li>

	<li class="<?php echo $this->errorMsg['partner_email'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['partner_email'] ? 'error' : ''?>">Email</label>
	    <div>
		<span>
		    <input id="partner_email" name="partner_email" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['partner_email']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['partner_email'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['partner_email']?></p>
	</li>
	
	<li class="<?php echo $this->errorMsg['partner_phone'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['partner_phone'] ? 'error' : ''?>">Tel.</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="partner_phone_prefix" name="partner_phone_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['partner_phone_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="partner_phone_number" name="partner_phone_number" type="text" size="10" value="<?php echo $this->data['partner_phone_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['partner_phone'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['partner_phone']?></p>
	</li>
	
	<li class="<?php echo $this->errorMsg['partner_fax'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['partner_fax'] ? 'error' : ''?>">Fax</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="partner_fax_prefix" name="partner_fax_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['partner_fax_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="partner_fax_number" name="partner_fax_number" type="text" size="10" value="<?php echo $this->data['partner_fax_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['partner_fax'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['partner_fax']?></p>
	</li>
	<li class="complex <?php echo $this->errorMsg['partner_address'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['partner_address'] ? 'error' : ''?>">Indirizzo</label>
	    <div>
		<span class="input">
		    <input class="field text addr" name="partner_street" id="partner_street" size="50" type="text" value="<?php echo $this->data['partner_street']?>" />
		    <label>Via</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="partner_street_number" id="partner_street_number" size="2" type="text" value="<?php echo $this->data['partner_street_number']?>" />
		    <label>N&deg;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="partner_loc" id="partner_loc" size="50" type="text" value="<?php echo $this->data['partner_loc']?>" />
		    <label>Localit&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="partner_city" id="partner_city" size="50" type="text" value="<?php echo $this->data['partner_city']?>" />
		    <label>Citt&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="partner_cap" id="partner_cap" size="10" type="text" value="<?php echo $this->data['partner_cap']?>" />
		    <label>CAP</label>
		</span>

		<span class="left">
		    <select class="field select addr" name="partner_province_id" id="partner_province_id">
			<option value=""></option>
			<?php foreach( $this->province AS $k => $provincia ): ?>
			<option value="<?php echo $k?>" <?php echo ( $this->data['partner_province_id'] == $k ) ? 'selected="selected"' : ''?>><?php echo $provincia['province_long_name']?></option>
			<?php endforeach; ?>
		    </select>
		    <label>Provincia</label>
		</span>
		<p class="error" style="<?php echo $this->errorMsg['partner_address'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['partner_address']?></p>
	    </div>
	</li>
	
	<li class="section">
	    <h3></h3>
	</li>	
	
	<li class="<?php echo $this->errorMsg['partner_notes'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['partner_notes'] ? 'error' : ''?>">Note</label>
	    <div>
		<span>
		    <textarea id="partner_notes" class="field textarea small" name="partner_notes" rows="3" cols="100"><?php echo  htmlentities( $this->data['partner_notes'] ) ?></textarea>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['partner_notes'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['partner_notes']?></p>
	</li>

    </ul>
    <div id="formButtons">
	<button id="salva" type="submit" name="salva" value="salva" class="button positive">
	    <img src="/images/admin/tick.png" alt="" />Continua
	</button>
    </div>
</form>
